package com.fhzn.boss.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fhzn.boss.entity.User;
import com.fhzn.boss.enums.Constants;
import com.fhzn.boss.enums.WebResponseEnum;
import com.fhzn.boss.mapper.UserMapper;
import com.fhzn.boss.service.OperatorLogService;
import com.fhzn.boss.service.UserService;
import com.fhzn.boss.tools.AESEncryptorUtils;
import com.fhzn.boss.web.converter.UserConverter;
import com.fhzn.boss.web.request.UserLoginRequest;
import com.fhzn.boss.web.request.UserOpRequest;
import com.fhzn.boss.web.request.UserQueryRequest;
import com.fhzn.boss.web.vo.UserVO;
import com.fhzn.commons.toolkit.exception.BuzException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author 老顾
 * @apiNote
 * @since 2023/12/22
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    private final OperatorLogService operatorLogService;
    private final UserMapper userMapper;


    @Override
    public Page<User> userWithPage(UserQueryRequest request) {
        request.checkParams();
        request.rewriteParams();
        request.getOffset();
        request.initQueryTime();

        return userMapper.query(request);
    }

    @Override
    public UserVO add(UserOpRequest request) {
        request.checkBaseParams();
        request.setUserType(1);
        request.checkAdd();
        if (StringUtils.isAnyBlank(request.getNickname())) {
            throw new BuzException("请求参数错误");
        }
        // 校验添加用户名的唯一性
        if (userMapper.findUserByNickName(request.getNickname()) != null) {
            throw new BuzException("当前用户已存在");
        }
        User user = request.addGenServiceParam();
        try {
            userMapper.insert(user);
        } catch (Exception e) {
            throw new BuzException("新增用户失败，用户信息重复！");
        }

        // 记录操作日志
        User userInfo = userMapper.findUserByNickName(request.getNickname());
        operatorLogService.operatorLog(request.getHandler(), Constants.USER_SECTION, Constants.USER_ADD_METHOD, request.getNickname(), null, userInfo, Constants.APP_CODE);
        return UserConverter.fromUser(user);
    }

    @Override
    public String updateUser(UserOpRequest request) {
        request.checkBaseParams();
        request.setUserType(1);
        request.checkUpdate();

        User userBefore = userMapper.selectById(request.getId());
        try {
            userMapper.updateById(request.updateGenServiceParam());
        } catch (Exception e) {
            throw new BuzException("更新用户失败，用户信息重复！");
        }

        // 记录操作日志
        operatorLogService.operatorLog(request.getHandler(), Constants.USER_SECTION, Constants.USER_UPDATE_METHOD, userBefore.getNickname(), userBefore, userBefore, Constants.APP_CODE);

        return WebResponseEnum.SUCCESS.getMessage();
    }

    @Override
    public String login(UserLoginRequest request) {
        if (StringUtils.isAnyBlank(request.getUsername(), request.getPassword())) {
            throw new BuzException("请求参数错误，请检查后重试");
        }
        User user = userMapper.findUserByNickName(request.getUsername());
        if (null == user) {
            throw new BuzException("用户不存在");
        }
        if (!StringUtils.equals(request.getUsername(), user.getNickname())) {
            throw new BuzException("用户名错误");
        }
        if (!StringUtils.equals(AESEncryptorUtils.decrypt(user.getPassword()), request.getPassword())) {
            throw new BuzException("用户密码错误");
        }
        if (Constants.DISABLE == user.getStatus()) {
            throw new BuzException("当前用户账号已被禁用");
        }

        if (StringUtils.isAnyBlank(request.getPassword(), user.getPassword()) || null == user.getPasswordExpired()) {
            throw new BuzException("当前用户账号信息有异常，请联系管理员处理");
        }
        if (!Objects.equals(user.getAppCode(), Constants.APP_CODE)) {
            throw new BuzException("当前用户非本系统用户，登录失败");
        }
        if (user.getNickname() != null && StringUtils.equals(AESEncryptorUtils.decrypt(user.getPassword()), request.getPassword())) {
            return "用户 " + request.getUsername() + " 登录成功";
        } else {
            throw new BuzException("登录失败");
        }

    }

    @Override
    public User findUserByNickName(String nickname) {
        if (StringUtils.isBlank(nickname)) {
            throw new BuzException("用户" + nickname + "不存在");
        }
        return userMapper.findUserByNickName(nickname);
    }

}
